﻿using System;
using System.Diagnostics;
using System.ServiceModel;
using System.ServiceProcess;
using System.Threading;
using AccessControl.FingerPrintEngine;
using AccessControl.WorkflowEngine;


namespace AccessControl.AccessControlService
{
    public partial class ACService : ServiceBase
    {
        private ServiceHost _workflowHost;
        private ServiceHost _fingerPrintHost;

        public ACService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            EventLog.WriteEntry("AC Service", "Starting AC Service", EventLogEntryType.Information);
            try
            {
                _fingerPrintHost = new ServiceHost(typeof(FingerPrintEngineService));
                _fingerPrintHost.Open();
                EventLog.WriteEntry("AC Service", "FingerPrintService Started", EventLogEntryType.Information);
            }
            catch (Exception ex)
            {
                EventLog.WriteEntry("AC Service", ex.ToString(), EventLogEntryType.Error);
            }
            try
            {
                _workflowHost = new ServiceHost(typeof(WorkflowEngineService));
                _workflowHost.Open();
                EventLog.WriteEntry("AC Service", "WorkflowEngineService Started", EventLogEntryType.Information);
            }
            catch (Exception ex)
            {
                EventLog.WriteEntry("AC Service", ex.ToString(), EventLogEntryType.Error);
            }
            EventLog.WriteEntry("AC Service", "ALL AC Service Started", EventLogEntryType.Information);

        }

        protected override void OnStop()
        {
            try
            {
                _fingerPrintHost.Close();
                EventLog.WriteEntry("AC Service", "FingerPrintSerice Stopped", EventLogEntryType.Information);
                _workflowHost.Close();
                EventLog.WriteEntry("AC Service", "WorkflowEngineService Stopped", EventLogEntryType.Information);
                EventLog.WriteEntry("AC Service", "ALL AC Services Stopped", EventLogEntryType.Information);
            }
            catch (Exception ex)
            {
                EventLog.WriteEntry("AC Service", ex.ToString(), EventLogEntryType.Error);
            }
        }
    }
}
